//
//  MovieViewController.m
//  UI14_豆瓣完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieTableViewCell.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"
#import "MovieViewController1.h"
#import "MyViewController.h"

@interface MovieViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) NSMutableArray *tempArr;

@end

@implementation MovieViewController

- (void)dealloc {
    [_tableView release];
    [_dataArr release];
    [_tempArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    
    self.title = @"电影";
    
    self.navigationController.navigationBar.translucent = NO;
    // 数据解析
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    self.tempArr = [NSMutableArray arrayWithContentsOfFile:path];
    
    self.dataArr = [NSMutableArray array];
    
    for (NSDictionary *dic in _tempArr) {
        MovieModel *model = [MovieModel movieModel];
        [model setValuesForKeysWithDictionary:dic];
        [_dataArr addObject:model];
        
    }
    
    // 创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView release];
    
    // 注册重用池
    [_tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)itemAction:(UIBarButtonItem *)item {
    
    MyViewController *myVC = [[MyViewController alloc] init];
//    myVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//    [self presentViewController:myVC animated:YES completion:nil];
    
    [self.navigationController pushViewController:myVC animated:YES];

    [myVC release];
}
// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    MovieModel *model = _dataArr[indexPath.row];
    cell.label.text = model.title;
    
    NSString *str = model.image;
    
    [cell.imageView2 sd_setImageWithURL:[NSURL URLWithString:str]];
    
    return cell;
    
}


// Cell 的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieViewController1 *movVc = [[MovieViewController1 alloc] init];
    movVc.b = NO;
    [self.navigationController pushViewController:movVc animated:YES];
    
    NSDictionary *dic = _tempArr[indexPath.row];
    movVc.DIC = dic;
    
    [movVc release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
